package com.gmail.dengtao.joe.group.interceptor.impl;

import com.gmail.dengtao.joe.group.channel.Channel;
import com.gmail.dengtao.joe.group.channel.ChannelListener;
import com.gmail.dengtao.joe.group.interceptor.Interceptor;
import com.gmail.dengtao.joe.group.interceptor.InterceptorEntity;
import com.gmail.dengtao.joe.group.member.IdleStatus;
import com.gmail.dengtao.joe.group.member.Member;
import com.gmail.dengtao.joe.group.member.MembershipListener;

public class TailInterceptor implements Interceptor {

	private MembershipListener membershipListener;
	private ChannelListener channelListener;

	public TailInterceptor() {
		super();
	}

	public TailInterceptor(MembershipListener membershipListener, ChannelListener channelListener) {
		super();
		if (membershipListener == null) {
			throw new IllegalArgumentException("The MembershipListener for this Receiver was not set!");
		}
		if (channelListener == null) {
			throw new IllegalArgumentException("The ChannelListener for this Receiver was not set!");
		}
		this.membershipListener = membershipListener;
		this.channelListener = channelListener;
	}

	@Override
	public void init() {
		// Empty
	}

	@Override
	public void destroy() {
		// Empty
	}

	@Override
	public void memberAdded(InterceptorEntity interceptorEntity, Channel channel, Member member) throws Exception {
		if (membershipListener != null) {
			membershipListener.memberAdded(channel, member);
		}
	}
	
	@Override
	public void memberDisappeared(InterceptorEntity nextEntity, Channel channel, Member member) throws Exception {
		if (membershipListener != null) {
			membershipListener.memberDisappeared(channel, member);
		}
	}

	@Override
	public void memberIdle(InterceptorEntity nextEntity, Channel channel, Member member,
			IdleStatus status) throws Exception {
		if (membershipListener != null) {
			membershipListener.memberIdle(channel, member, status);
		}
	}

	@Override
	public void exceptionCaught(InterceptorEntity nextEntity, Channel channel, Member member,
			Throwable cause) {
		if (channelListener != null) {
			channelListener.exceptionCaught(channel, member, cause);
		}
	}

	@Override
	public void dataReceived(InterceptorEntity nextEntity, Channel channel, Member sender,
			Object data) throws Exception {
		if (channelListener != null) {
			channelListener.dataReceived(channel, sender, data);
		}
	}

	@Override
	public void dataSent(InterceptorEntity nextEntity, Channel channel, Member target,
			Object data) throws Exception {
		if (channelListener != null) {
			channelListener.dataSent(channel, target, data);
		}
	}

	@Override
	public void dataNotSent(InterceptorEntity nextEntity, Channel channel, Member member,
			Object data) throws Exception {
		if (channelListener != null) {
			channelListener.dataNotSent(channel, member, data);
		}
	}
	
	public void setMembershipListener(MembershipListener membershipListener) {
		this.membershipListener = membershipListener;
	}

	public void setChannelListener(ChannelListener channelListener) {
		this.channelListener = channelListener;
	}
}
